﻿var close_room = function () {
    document.getElementById("msgDiv").style.display = 'none';
}

$("document").ready(function () {
    $("#query_room").mouseup(function () {
        $("#loading_img").show();
        var begin_time = $("#text_begin").val();
        var end_time = $("#text_end").val();
        var room_capacity = $("#text_capacity").val();
        if (begin_time == "")
            alert("请选择开始时间");
        else if (end_time == "")
            alert("请选择结束时间");
        else if (room_capacity == "")
            alert("请输入会议容量");
        else if (begin_time > end_time)
            alert("开始时间大于结束时间!");
        else {
            $.ajax({
                url: "Ajax/QueryRoom.aspx",
                type: "post",
                dataType: "json",
                data: { begin: begin_time, end: end_time, capacity: room_capacity },
                success: function (msg) {
                    $("#loading_img").hide();
                    $("table#room_table tr:gt(0)").remove();
                    $(msg).each(function () {
                        var id = this.id;
                        var button = "<input type='button' class='book_button' id=" + id + " value='预订'></input>";
                        var row = "<tr id=" + id + "><td align='center'>" + this.id + "</td><td align='center'>" + this.capacity + "</td><td align=center>" + button + "</td></tr>";
                        var row_num = $("table#room_table tr").size() - 1;
                        $(row).insertAfter($("table#room_table tr:eq("+row_num+")"));
                    })
                    $("input[type='button'].book_button").mouseup(function () {
                        var id = $(this).attr("id");
                        var user_id = $("#roomuser_id").val();
                        var result;
                        if (user_id == "")
                            alert("请输入待借用户ID");
                        else {
                            var emploee_id = $("#roomuser_id").val();
                            $.ajax({
                                url: "Ajax/CheckEmploee.aspx",
                                type: "post",
                                datatype: "json",
                                data: { user_id: emploee_id },
                                success: function (msg) {
                                    result = parseInt(msg);
                                    if (result == 1) {
                                        
                                         $.ajax({
                                                 url: "Ajax/BookRoom.aspx",
                                                type: "post",
                                                data: { id: id, begin: begin_time, end: end_time ,userid:user_id},
                                                success: function (msg) {
                                                alert(msg);
                                                $("tr#" + id + "").remove();
                                                }
                                          })
                                    }
                                     else
                                        alert("请输入正确的员工ID");
                                 }
                             });
                            }
                        }); 
                  }  
            });  
        }
     });
 });   
